package org.smile.excel.jdbc;

import org.smile.dataset.Row;
import org.smile.expression.Context;
import org.smile.expression.DefaultContext;
import org.smile.expression.Engine;

public class ExpressColumn extends ExcelSqlExpress<Object>{
	//默认的解析上下文用于解析列表达式内容
	private Context ctx=new DefaultContext();
	//列表达式
	String expression;
	//别名
	String alias;
	
	public ExpressColumn(String exp,String alias){
		this.expression=exp;
		this.alias=alias;
	}
	
	public ExpressColumn(String exp){
		this.expression=exp;
	}
	
	public String getColumnLabel(){
		return alias==null?expression:alias;
	}

	@Override
	public Object eval(Row row) {
		ctx.setRoot(row);
		return Engine.getInstance().evaluate(ctx, expression);
	}

	@Override
	public String toString() {
		return this.expression+(this.alias==null?"":" "+this.alias);
	}
	
	
}
